/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.textdescriptor;

import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import com.acarter.scenemonitor.information.A_MonitorInformationPanel;


/**
 * @author Carter
 *
 */
public class TextInformation extends A_MonitorInformationPanel {

	/** Logger Name */
	public static final String LOGGER_NAME = "com.acarter.scenemonitor.textdescriptor.TextInformation";

	/** Logger */
	private static final Logger LOGGER = Logger.getLogger(TextInformation.LOGGER_NAME);

	/** Descriptor handling for each tree object */
	private A_TextDescriptorHandler descriptorHandler;

	/** Document in the info text area */
	private StyledDocument infoDocument;

	/**
	 * 
	 */
	public TextInformation() {

		if(A_TextDescriptorHandler.descriptorsExist()) {
			
			// Look for the jME Physics library
			try {
				Class.forName("com.jmex.physics.PhysicsSpace");
	
				descriptorHandler = new PhysicsDescriptorHandler();
				LOGGER.info("jME Physics 2 found, using a Scene Monitor physics descriptor handler.");
	
			} catch (ClassNotFoundException e) {
	
				descriptorHandler = new StandardDescriptorHandler();
				LOGGER.info("jME Physics 2 not found, using a Scene Monitor standard descriptor handler.");
			}
		}
		else
			descriptorHandler = new EmptyDescriptorHandler();
		
	}
	
	/**
	 * @see com.acarter.scenemonitor.dialog.A_MonitorInformationPanel#buildInformationPanel()
	 */
	@Override
	public JPanel buildInformationPanel() {

		JPanel infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));

		JTextPane textPane = new JTextPane();
		textPane.setEditable(false);
		((DefaultCaret)textPane.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
		infoDocument = textPane.getStyledDocument();

		// Initialize some styles.
		Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

		Style regular = infoDocument.addStyle("regular", def);
		StyleConstants.setFontFamily(def, "SansSerif");

		Style s = infoDocument.addStyle("italic", regular);
		StyleConstants.setItalic(s, true);

		s = infoDocument.addStyle("bold", regular);
		StyleConstants.setBold(s, true);

		s = infoDocument.addStyle("small", regular);
		StyleConstants.setFontSize(s, 10);

		s = infoDocument.addStyle("large", regular);
		StyleConstants.setFontSize(s, 16);

		JScrollPane infoScrollPane = new JScrollPane(textPane);
		infoPanel.add(infoScrollPane);

		return infoPanel;
	}

	/**
	 * @see com.acarter.scenemonitor.dialog.A_MonitorInformationPanel#updateInformation(java.lang.Object)
	 */
	@Override
	public void updateInformation(Object object) {

		A_TextDescriptor descriptor = descriptorHandler.getDescriptor(object);

		if (descriptor != null)
			descriptor.updateInfo(infoDocument, object);
	}
	
	/**
	 * Returns the test descriptor handler so new descriptors may be added.
	 * 
	 * @return A_TextDescriptorHandler
	 */
	public A_TextDescriptorHandler getTextDescriptorHandler() {
		
		return descriptorHandler;
	}
}
